package com.atguigu.yygh.hosp.service.impl;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Department department) {
        if (department == null) {
            throw new YyghException(20001, "数据为空!");
        }

        Department existDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        //较医院添加作出修改 有局限性比如:字段不同
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        if (existDepartment != null) {
            department.setId(existDepartment.getId());//id设置
            departmentRepository.save(department);
        } else {
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> selectPage(int page, int limimt, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        //当前页 从0开始
        Pageable pageable = PageRequest.of(page - 1, limimt, sort);
        //创建匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        //条件封装
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pageModel = departmentRepository.findAll(example, pageable);
        return pageModel;
    }

    @Override
    public void removeDeptId(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            //这是物理删除 请改为逻辑删除
//            departmentRepository.deleteById(department.getId());
            //逻辑删除 我只用了10秒钟~~~~~~~
            department.setIsDeleted(1);
            departmentRepository.save(department);
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号查询下属所有科室  每个科室包含一个bigCode  就是它所属的大科室
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表
        List<Department> allDepartmentList = departmentRepository.findAll(example);

        //通过流进行分组处理       key是Bigcode  value是下属所有所有科室
        Map<String, List<Department>> deparmentMap = allDepartmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String, List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> departmentListSmall = entry.getValue();
            //封装大科室  为什么封装?因为mongo的字段比页面展示的字段多
            DepartmentVo departmentVoBig = new DepartmentVo();
            departmentVoBig.setDepcode(bigcode);
            departmentVoBig.setDepname(departmentListSmall.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departmentListSmall) {
                DepartmentVo departmentVoSmall = new DepartmentVo();
                departmentVoSmall.setDepcode(department.getDepcode());
                departmentVoSmall.setDepname(department.getDepname());
                children.add(departmentVoSmall);
            }
            departmentVoBig.setChildren(children);
            result.add(departmentVoBig);
        }

        return result;
    }

    @Override
    public String getDepName(String hoscode,String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}